package com.lmx.repoDomain.repository;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.ServiceFinishLevel;
import com.lmx.repoDomain.tmodel.ServiceMerch;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@JsonApiResourceRepository(ServiceMerch.class)
@Component
public class ServiceMerchRepository extends BaseRepository<ServiceMerch> {

	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(ServiceMerch.class);
	}

	@Override
	@JsonApiSave
	public ServiceMerch save(ServiceMerch data) {
		if (data.getId() == null) {// create
			dslJpaService.create(data);
		} else {// update
			if (data.getDelStatus() == 1) {// 删除
				int deleteFlag = nursingService.checkServiceMerchRelations(data);
				if (deleteFlag > 0) {
					data.setErrcode(8);// no delete
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
				}
				dslJpaService.update(data);
			} else {
				dslJpaService.update(data);
			}
		}
		return data;
	}
}
